package cn.kgc.watch.web.admin;


import cn.kgc.watch.entity.*;
import cn.kgc.watch.entity.vo.PageInfoVo;
import cn.kgc.watch.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.util.*;

@RestController
@RequestMapping("admin")
public class
AdminLogin {
    @Autowired
    private AdminService adminService;
    @Autowired
    private CommoditySpuService commoditySpuService;
    @Autowired
    private CommoditySkuService commoditySkuService;
    @Autowired
    private PictureService pictureService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private BrandService brandService;
    //管理员登录
    @PostMapping("loginAdmin")
    public ResultInfo loginAdmin(@RequestBody Admin admin, HttpSession httpSession){
        admin = this.adminService.findAdimin(admin);
        if(admin!=null){
            httpSession.setAttribute("admin",admin);
        }
        return new ResultInfo(200,"成功",admin);
    }
    //退出登录
    @GetMapping("outLogin")
    public ResultInfo outLogin(HttpSession httpSession){
        httpSession.removeAttribute("admin");
        return new ResultInfo(200,"成功");
    }
    //商品上架下架
    @PutMapping("modifySaleable")
    public ResultInfo modifySaleable(@RequestBody CommoditySpu commoditySpu){
        this.commoditySpuService.modifySaleable(commoditySpu);
        return new ResultInfo(200,"成功");
    }
    //添加商品返回主键
    @PostMapping("addGoods")
    public ResultInfo addGoods(@RequestBody CommoditySku commoditySku){
        return new ResultInfo(200,"添加成功",this.commoditySkuService.addSku(commoditySku));
    }
    //添加商品图片
    @PostMapping("addPicture")
    public ResultInfo addPicture(@RequestParam Map map, @RequestParam(value= "pic")MultipartFile[] pic) throws Exception{
        String extendsName=null;
        Integer skuId = Integer.parseInt((String) map.get("skuId"));
        String path= ResourceUtils.getURL("classpath:").getPath();
        Integer master = this.pictureService.countMasterPic(skuId);
        Picture picture = null;
        CommoditySku commoditySku = new CommoditySku();
        commoditySku.setId(Long.valueOf(skuId));
        File file=new File(path);
            String serverDirectory=file.getAbsolutePath()+File.separator+"static"+File.separator+"picture"+File.separator; //上传路径
            String fileName;
            File destination;
            if(pic!=null){
                for(MultipartFile thePic: pic) {
                    if (!thePic.isEmpty()) {
                        //获取文件的后缀名
                        extendsName = thePic.getOriginalFilename().substring(thePic.getOriginalFilename().lastIndexOf("."));
                        //给文件上传的名字
                        fileName = UUID.randomUUID().toString().replace("-","");
                        fileName = fileName + extendsName; //给图片命名
                        destination=new File(serverDirectory,fileName);
                        thePic.transferTo(destination);
                            picture = new Picture();
                            picture.setPictureName(fileName);
                            picture.setIsMain(0);
                            picture.setCommoditySku(commoditySku);
                            picture.setCreateTime(new Date());
                            this.pictureService.addPic(picture);
                    }
                }
            }

        return new ResultInfo(200,"成功");
    }
    //查询所有的spu
    @GetMapping("adminCommoditySpu")
    public  ResultInfo adminCommoditySpu(PageInfoVo pageInfoVo){
        return new ResultInfo(200,"成功",this.commoditySpuService.findAdminCommoditySpu(pageInfoVo));
    }
    //查询所有的分类
    @GetMapping("adminCategoryAll")
    public ResultInfo adminCategoryAll(){
        return new ResultInfo(200,"成功",this.categoryService.findAll());
    }
    //查看所有的品牌
    @GetMapping("adminBrandAll")
    public ResultInfo adminBrandAll(){
       return new ResultInfo(200,"成功",this.brandService.findAll());
    }
    //添加spu商品
    @PostMapping("adminAddCommoditySpu")
    public ResultInfo adminAddCommoditySpu(@RequestBody CommoditySpu commoditySpu){
        this.commoditySpuService.addCommoditySpu(commoditySpu);
        return new ResultInfo(200,"成功");
    }
    @PutMapping("adminModifyCommoditySku")
    public ResultInfo adminModifyCommoditySku(@RequestBody CommoditySku commoditySku){
        this.commoditySkuService.adminModifyCommoditySku(commoditySku);
        return new ResultInfo(200,"成功");
    }

    @GetMapping("adminByIdCommoditySku")
    public ResultInfo adminByIdCommoditySku(Integer id){
        return new ResultInfo(200,"成功",this.commoditySkuService.findAdminByIdCommoditySku(id));
    }
}
